<?php namespace simpleFlow\view\html;

use simpleFlow\Object as SFObject;

/**
 * Html field component
 * @package simpleFlow\view
 * @subpackage html
 * @author Guilherme Fontenele
 */
class HtmlField extends SFObject
{
	/**
	 * @var string Field name 
	 */
	public $strName;
	/**
	 * @var string Tag name
	 */
	public $strTag;
	/**
	 * @var boolean If tag closed
	 */
	public $booTagClosed;
	/**
	 * @var array Attributes
	 */
	public $arrAttributes = array();
	/**
	 * @var array Stylesheet Classes
	 */
	public $arrClass = array();
	/**
	 * @var string Content inside component
	 */
	public $strContent;
	/**
	 * @var string Field value
	 */
	public $strValue;
	/**
	 * @var object simpleFlow\view\html\Label
	 */
	public $objLabel;
	
	/**
	 * Constructor
	 * @param string $strTag Tag name
	 * @param string $strValue Value
	 * @param string $strName Field name
	 * @param boolean $booTagClosed If tag is closed
	 */
	public function __construct($strTag,$strValue = null,$strName = null,$booTagClosed = false)
	{
		$this->strTag = $strTag;
		$this->strValue = $strValue;
		$this->strName = $strName;
		$this->booTagClosed = $booTagClosed;
		$this->configure();
	}
	
	/**
	 * Interface of configure method
	 */
	public function configure() { }
	
	/**
	 * Add a Stylesheet class
	 * @param string $strClass CSS class name
	 */
	public function addClass($strClass)
	{
		$this->arrClass[$strClass] = $strClass;
	}
	
	/**
	 * Add an attribute
	 * @param string $strAttribute Attribute name
	 * @param string $strValue Value
	 */
	public function addAttribute($strAttribute,$strValue)
	{
		$this->arrAttributes[$strAttribute] = $strValue;
	}
	
	/**
	 * Convert the object to string
	 * @return string Html content
	 */
	public function __toString()
	{
		$_strHtml = "";
		
		if($this->objLabel && get_class($this->objLabel) != 'Label')
		{
			$_strHtml.= $this->objLabel->__toString();
		}
		
		$_strHtml.= "<{$this->strTag} ";
		
		switch(true)
		{
			case $this->strName:
				$_strHtml.= "name='{$this->strName}' ";
				$_strHtml.= "id='{$this->strName}' ";
			
			case (count($this->arrClass) > 0):
				$_strHtml.= "class='";
				foreach($this->arrClass as $_strValue)
				{
					$_strHtml.= "{$_strValue} ";
				}
				$_strHtml.= "' ";
			case (count($this->arrAttributes) > 0):
				foreach($this->arrAttributes as $_strAttr => $_strValue)
				{
					$_strHtml.= "{$_strAttr}='{$_strValue}' ";
				}
		}
		
		if($this->booTagClosed)
		{
			if($this->strValue)
			{
				$_strHtml.= "value='{$this->strValue}'";
			}
			$_strHtml.= "/>";
		}else{
			$_strHtml.= ">";
			$_strHtml.= $this->strContent;
			$_strHtml.= "</{$this->strTag}>";
		}
		
		return $_strHtml;
	}
	
	/**
	 * Create a label to the field
	 * @param string $strLabel Content of label
	 */
	public function createLabel($strLabel)
	{
		$this->objLabel = new Label($this->strName, $strLabel);
	}
}

?>